<template>
    <div>
        <el-table :data="studentsList" style="width: 100%">
            <el-table-column label="学号" prop="userNumber" fixed>
            </el-table-column>
            <el-table-column label="姓名" prop="name" fixed>
            </el-table-column>
            <el-table-column label="性别" prop="gender">
            </el-table-column>
            <el-table-column label="年龄" prop="age">
            </el-table-column>
            <el-table-column label="电话" prop="phone">
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <el-tag size="medium" :type="scope.row.state == '通过' ? 'success' : 'danger'">{{ scope.row.state
                    }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="备注">
                <template slot-scope="scope">
                    <el-popover trigger="click" placement="top" title="备注">
                        <template v-if="scope.row.state == '未通过'">
                            <p>拒绝原因:{{ scope.row.unJoin }}</p>
                        </template>
                        <template v-else>
                            <p>无</p>
                        </template>
                        <div slot="reference" class="name-wrapper">
                            <el-tag type="info" size="medium">查看详情</el-tag>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column align="right" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row.userNumber)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination style="width:50%;margin: 20px auto 0;" background @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize" layout=" prev, pager, next, jumper,total, sizes" :total="total">
        </el-pagination>



        <el-dialog title="教师信息修改" :visible.sync="dialogFormVisible" width="25%">
            <el-form :model="form" :rules="rules" ref="ruleForm">
                <el-form-item label="教师工号" :label-width="formLabelWidth" prop="userNumber">
                    <el-input v-model="form.userNumber" autocomplete="off" placeholder="工号由入学年份+院系+班级+序号"></el-input>
                </el-form-item>
                <el-form-item label="教师名称" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="form.name" autocomplete="off" placeholder="教师姓名"></el-input>
                </el-form-item>
                <el-form-item label="教师年龄" :label-width="formLabelWidth" prop="age">
                    <el-input v-model="form.age" autocomplete="off" placeholder="教师年龄 18-24"></el-input>
                </el-form-item>
                <el-form-item label="教师电话" :label-width="formLabelWidth" prop="phone">
                    <el-input v-model="form.phone" autocomplete="off" placeholder="教师电话"></el-input>
                </el-form-item>
                <el-form-item label="教师性别" :label-width="formLabelWidth" prop="gender">
                    <el-select v-model="form.gender" placeholder="请选择性别" style="text-align: left;">
                        <el-option label="男" value="男"></el-option>
                        <el-option label="女" value="女"></el-option>
                    </el-select>
                </el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import api from '@/api'
export default {
    // 生命周期钩子函数 一个页面或者项目的从生到死的过程会有这个函数进行检测
    mounted() {  // 表示页面元素挂载完毕
        //   console.log(1111,api);
        this.getTeachers();
        this.getClassListUnPage()
    },
    data() {
         //  正则表达式
         var phoneRegex = (rule, value, callback) => {
            let phoneRegex = /^1[356789][0-9]{9}$/;
            if (phoneRegex.test(value)) {
                callback();
            } else {
                callback(new Error('请输入正确的电话号码'));

            }
        };
        var userNumberRegex = (rule, value, callback) => {
            let userNumberRegex = /^20[0-9]{2}[0-9]{5}[0-9][0-9][1-9]$/;
            if (userNumberRegex.test(value)) {
                callback();
            } else {
                callback(new Error('请输入正确的工号'));

            }
        };
        var ageRegex = (rule, value, callback) => {
            // 18 - 24 
            let ageRegex = /^[2-6][0-9]$/;
            if (ageRegex.test(value)) {
                callback();
            } else {
                callback(new Error('请输入正确的年龄'));

            }
        };
        return {
            pageIndex: 1,
            pageSize: 10,
            total: 10,
            studentsList: [],
            dialogFormVisible: false,
            form: {
                userNumber: "",
                name: '',
                age: "",
                gender: "男",
                phone: "",
                classId: "6507a6161c150000de003082"
            },
            rules: {
                userNumber: [
                    { required: true, message: '请输入工号', trigger: 'blur' },
                    { validator: userNumberRegex, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                ],
                age: [
                    { required: true, message: '请输入年龄', trigger: 'blur' },
                    { validator: ageRegex, trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入电话号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度是 11 字符', trigger: 'blur' },
                    { validator: phoneRegex, trigger: 'blur' }
                ],
                gender: [
                    { required: true, message: '请选择性别', trigger: 'blur' },
                ],
                classId: [
                    { required: true, message: '请选择班级', trigger: 'blur' },
                ]
            },
            classList: [],
            formLabelWidth: '80px'
        }
    },
    methods: {
        // vue2中箭头函数反而少用 箭头函数的this指向采用的是就近原则 而vue中我们不想使用就近原则
        // vue2中特殊情况必须得用箭头函数 这个情况我们碰
        async getTeachers() {
            let res = await api.getTeachers({ pageIndex: this.pageIndex, pageSize: this.pageSize })
            if (res?.code == 200) {
                this.studentsList = res.data;
                this.total = res.total
            } else {
                this.studentsList = []
            }
        },
        async getClassListUnPage() {
            let res = await api.getClassListUnPage();
            if (res?.code == 200) {
                this.classList = res.data
            } else {
                this.classList = []
            }
        },
        handleEdit(userInfo) {
            this.dialogFormVisible = true
            this.form = { ...userInfo }
        },
        async handleDelete(userNumber) {
            let res = await api.delTeacher({ userNumber });
            if (res?.code == 200) {
                this.$message({
                    message: res.message,
                    type: 'success'
                });
                this.getTeachers();
            } else {
                this.$message.error(res.message);
            }
        },
        async handleOk() {
            this.dialogFormVisible = false;
            let res = await api.updateTeacher({ ...this.form })
            if (res?.code == 200) {
                this.$message({
                    message: res.message,
                    type: 'success'
                });
                this.getTeachers();
            } else {
                this.$message.error(res.message);
            }
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.getTeachers()
        },
        handleCurrentChange(val) {
            this.pageIndex = val;
            this.getTeachers()
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.handleOk();
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
}
</script>

<style lang="scss" scoped></style>